import com.min.gps.service.BackServer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;

import javax.net.SocketFactory;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class Client {
    Logger log = LogManager.getLogger(Client.class);

    AtomicInteger countConnect = new AtomicInteger(0);
    AtomicInteger countRespone = new AtomicInteger(0);
    private String host;
    private int port;


    public static void main(String[] args) throws Exception {

        Client client = new Client("127.0.0.1", 1122);
        client.test();
    }

    public void test() {
        ExecutorService pool = new ThreadPoolExecutor(0, 10000,
                20L, TimeUnit.SECONDS,
                new SynchronousQueue<>());
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    connect();
                }
            });
       while (!pool.isTerminated()) {
        }
        log.info(String.format("连接:%d  响应:%d", countConnect.get(), countRespone.get()));
        log.info("fff==============", countRespone.incrementAndGet()- countConnect.incrementAndGet());
    }


    public void connect()  {
   //while (true) {
            Socket socket = null;
            try {
                socket = SocketFactory.getDefault().createSocket(host, port);
                //socket.setSoTimeout(100);//设置超时
                //创建一个流套接字并将其连接到指定主机上的指定端口号
                countConnect.incrementAndGet();
                //读取服务器端数据

                    //向服务器端发送数据
                    DataInputStream  input= new DataInputStream(socket.getInputStream());
                    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                   byte[] loginBytes =new byte[] {(byte)0xAF,0x00,0x56,(byte)0xAF,0x00,0x01,0x01,0x24,0x47,0x4E,0x52,0x4D,0x43,0x2C,0x30,0x36,0x32,0x33,0x31,0x38,0x2E,0x38,0x32,0x33,0x2C,0x41,0x2C,0x32,0x32,0x37,0x37,0x2E,0x31,0x30,0x38,0x31,0x33,0x2C,0x4E,0x2C,0x31,0x31,0x33,0x31,0x39,0x2E,0x39,0x34,0x37,0x35,0x34,0x30,0x30,0x30,0x30,0x30,0x30,0x31,0x2C,0x45,0x2C,0x32,0x31,0x2E,0x33,0x37,0x34,0x2C,0x39,0x31,0x2E,0x37,0x37,0x2C,0x32,0x39,0x30,0x36,0x31,0x37,0x2C,0x2C,0x2C,0x44,0x2A,0x36,0x37,0x0D,0x0A,0x2D,0x3F,(byte)0xD3,(byte)0xFF};
                //  byte[] loginBytes =new byte[] {(byte)  0xAF,0x00,0x75,(byte) 0xAF,0x00,0x0A,0x02,0x55,0x00,0x02,0x09,0x00,0x60,0x55,0x00,0x02,0x03,0x00,0x5A,0x55,0x00,0x02,0x0C,0x00,0x63,0x55,0x00,0x02,0x04,0x00,0x5B,0x55,0x00,0x02,0x01,0x00,0x58,0x55,0x00,0x02,0x16,0x00,0x6D,0x55,0x00,0x02,0x12,0x00,0x69,0x55,0x00,0x02,0x07,0x00,0x5E,0x55,0x00,0x02,0x0A,0x00,0x61,0x55,0x00,0x02,0x02,0x00,0x59,0x55,0x00,0x02,0x08,0x00,0x5F,0x55,0x00,0x02,0x05,0x00,0x5C,0x55,0x00,0x02,0x10,0x00,0x67,0x55,0x00,0x02,0x13,0x00,0x6A,0x55,0x00,0x02,0x14,0x00,0x6B,0x55,0x00,0x02,0x0E,0x00,0x65,0x55,0x00,0x02,0x0D,0x00,0x64,0x55,0x00,0x02,0x0B,0x00,0x62,0x55,0x00,0x02,0x0F,0x00,0x66,(byte) 0xC4,0x65,(byte) 0xFF};
                   out.write(loginBytes);
                   out.flush();
                    String ret = input.readUTF();
                    log.info("客户端ret=============:" + ret);
                    countRespone.incrementAndGet();
                  Thread.sleep(3000);


            } catch (Exception e) {
                log.info("客户端异常:" + e.getMessage());
            }
            }
 // }
    public Client(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}
